package com.best.onegous.Adapter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.R;
import com.best.onegous.View.NumberProgressBar;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.ShangPinDetails;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HomeAdapter extends BaseAdapter {
	Intent intent;
	private List<JSONObject> data = new ArrayList<JSONObject>();
	private LayoutInflater layoutInflater;
	private Context context;

	public HomeAdapter(Context context, List<JSONObject> data) {
		this.context = context;
		this.data = data;
		this.layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHome viewhome = null;
		if (convertView == null) {
			viewhome = new ViewHome();
			convertView = layoutInflater.inflate(R.layout.home_item, null);
			viewhome.img = (ImageView) convertView.findViewById(R.id.img);
			viewhome.title = (TextView) convertView.findViewById(R.id.title);
			viewhome.numberbar = (NumberProgressBar) convertView.findViewById(R.id.numberbar);
			viewhome.jindu = (TextView) convertView.findViewById(R.id.jindu);
			viewhome.shengyu = (TextView) convertView.findViewById(R.id.shengyu);
			
			convertView.setTag(viewhome);
		} else {
			viewhome = (ViewHome) convertView.getTag();
		}
		final JSONObject item = data.get(position);
		try {
			Apps.imageLoader.displayImage(
					Common.PHOTOURL + item.getString("thumb"),viewhome.img,
					Apps.yuanimg);
			viewhome.title.setText(item.getString("title"));
			viewhome.numberbar.setMax(Integer.parseInt(item.getString("zongrenshu")));
			viewhome.numberbar.setProgress(Integer.parseInt(item.getString("canyurenshu")));
			viewhome.jindu.setText(item.getString("codePercent"));
			viewhome.shengyu.setText(item.getString("shenyurenshu"));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		viewhome.img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				intent = new Intent(context, ShangPinDetails.class);
				try {
					intent.putExtra("id", item.getString("id"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHome {
		private ImageView img;
		private TextView title,jindu,shengyu;
		private NumberProgressBar numberbar;

	}

}